package com.example.bbsbe.controller;

import com.example.bbsbe.entity.user;
import com.example.bbsbe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class UserController {
    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }
    @GetMapping("/api/users/")
    public ResponseEntity<?> getusers() {
        List<user> response = userService.findAll();
        return ResponseEntity.ok(response);
    }
    @PostMapping("/reg/")
    public ResponseEntity<?> register(@RequestBody Map<String, String> request) {
        String name = request.get("name");
        String password = request.get("password");
        String email = request.get("email");
        String phonenum = request.get("phonenum");

        Map<String, Object> response = userService.register(name, password, email, phonenum);
        return ResponseEntity.ok(response);
    }
    @PostMapping("/login/")
    public ResponseEntity<?> login(@RequestBody Map<String, String> credentials) {
        String username = credentials.get("username");
        String password = credentials.get("password");
        Map<String, Object> response = userService.login(username, password);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/getnum/")
    public ResponseEntity<?> getUserStats(@RequestBody Map<String, String> request) {
        String username = request.get("username");
        Map<String, Object> response = userService.getUserStats(username);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/edit/")
    public ResponseEntity<?> editUserInfo(@RequestBody Map<String, String> request) {
        String username = request.get("username");
        String avatar = request.get("avatar");
        String avatar1 = username+avatar;
        String nickname = request.get("nickname");
        String signature = request.get("signature");
        userService.updateUser(username, avatar1, nickname, signature);
        return ResponseEntity.ok(Map.of("condition", 1));
    }
}
